<template>
  <div>
    <el-card>
      <div slot="header">
        <span>商品添加</span>
      </div>
      <!-- 商品名称 -->
      <el-form ref="form" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="formData.name" placeholder="商品名称"></el-input>
        </el-form-item>
        <!-- 请选择商品分类 -->
        <el-form-item label="商品分类">
          <el-select v-model="formData.category" placeholder="请选择商品分类">
            <el-option label="面包" value="面包"></el-option>
            <el-option label="辣条" value="辣条"></el-option>
          </el-select>
        </el-form-item>
        <!-- 商品价格 -->
        <el-form-item label="商品价格">
          <el-input-number v-model="formData.price" :step="2"></el-input-number>
        </el-form-item>
        <!-- 商品图片 -->
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            :action="`${API}/users/avatar_upload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="formData.imgUrl" :src="formData.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 商品描述 -->
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="formData.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">添加商品</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {addGoodsReq} from '@/api/goods'
export default {
  data() {
    return {
      formData: {
        name: '',
        category: '', //分类
        imgUrl: '',
        price: '',
        goodsDesc: '',
      },
      API: 'http://sell.h5.itsource.cn:8087',
    }
  },
  methods: {
    //头像
    async handleAvatarSuccess(res) {
      // console.log('res :>> ', res)
      //解构数据
      let {code, msg, imgUrl} = res
      //判断
      if (code === 0) {
        //数据回显
        this.formData.imgUrl = imgUrl
      }
    },

    //点击添加商品发送
    async submit() {
      //  数据
      let imgUrl = this.formData.imgUrl
      let category = this.formData.category
      let name = this.formData.name
      let price = this.formData.price
      let goodsDesc = this.formData.goodsDesc
      //发送请求
      let res = await addGoodsReq({
        ...this.formData,
        imgUrl,
        category,
        name,
        price,
        goodsDesc,
      })
    },
    beforeAvatarUpload() {},
    handleRemove(file, fileList) {},
    handlePictureCardPreview(file) {},
  },
}
</script>

<style lang="scss" scoped>
.el-input,
.el-textarea {
  width: 400px;
}
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
.rate {
  :deep(.el-form-item__content) {
    margin-left: 0 !important;
  }
}
</style>
